package com.sicau.socialpracticesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicau.socialpracticesystem.entity.param.PageParam;
import com.sicau.socialpracticesystem.entity.param.team.TeamQueryParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicau.socialpracticesystem.entity.param.team.TeamRouteQueryParam;
import com.sicau.socialpracticesystem.entity.po.Team;
import com.sicau.socialpracticesystem.entity.po.TeamMember;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.entity.vo.team.TeamVO;
import com.sicau.socialpracticesystem.entity.vo.team.TeamRouteVO;
import com.sicau.socialpracticesystem.mapper.TeamMapper;
import com.sicau.socialpracticesystem.mapper.TeamMemberMapper;
import com.sicau.socialpracticesystem.mapper.VOMapper;
import com.sicau.socialpracticesystem.service.TeamService;
import com.sicau.socialpracticesystem.util.PageUtil;
import com.sicau.socialpracticesystem.util.QueryWrapperUtil;
import com.sicau.socialpracticesystem.util.InverseGeoUtil;
import com.sicau.socialpracticesystem.util.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 支队表 服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {

    @Resource
    TeamMapper teamMapper;

    @Resource
    private VOMapper.TeamVOMapper teamVOMapper;

    @Resource
    private TeamMemberMapper teamMemberMapper;

    @Override
    public PageDataVO<TeamVO> queryTeamByConditions(TeamQueryParam teamQueryParam) {
        QueryWrapper<TeamVO> queryWrapper = QueryWrapperUtil.getQueryWrapper(TeamVO.class, TeamQueryParam.class, teamQueryParam);
        queryWrapper.lambda()
                .ge(teamQueryParam.getStartTime() != null, TeamVO::getStartTime, teamQueryParam.getStartTime())
                .le(teamQueryParam.getEndTime() != null, TeamVO::getEndTime, teamQueryParam.getEndTime())
                .orderByDesc(TeamVO::getId);
        Page<TeamVO> infoVOPage = new Page<>(teamQueryParam.getCurrentPage(), teamQueryParam.getPageSize());
        infoVOPage = teamVOMapper.selectPage(infoVOPage, queryWrapper);
        return PageUtil.convert(infoVOPage);
    }


    @Override
    public List<TeamRouteVO> queryAllTeamRoute(TeamRouteQueryParam param) {
        LambdaQueryWrapper<Team> queryWrapper = new LambdaQueryWrapper<Team>()
                .ge(!ObjectUtils.isEmpty(param.getStartTime()), Team::getStartTime, param.getStartTime())
                .le(!ObjectUtils.isEmpty(param.getEndTime()), Team::getEndTime, param.getEndTime())
                .orderByDesc(Team::getId);
        List<Team> teamList = teamMapper.selectList(queryWrapper);

        ArrayList<TeamRouteVO> teamRouteList = new ArrayList<>();
        if (teamList != null && teamList.size() > 0) {
            teamList.forEach(item -> {
                TeamRouteVO teamRouteVO = new TeamRouteVO();
                teamRouteVO.setTeamName(item.getTeamName());
                teamRouteVO.setOrigin(new InverseGeoUtil.LatLng(item.getOriginLat(), item.getOriginLng()));
                teamRouteVO.setDestination(new InverseGeoUtil.LatLng(item.getDestinationLat(), item.getDestinationLng()));
                teamRouteList.add(teamRouteVO);
            });
        }
        return teamRouteList;
    }

    @Override
    public TeamVO queryTeamById(Integer id) {
        return teamVOMapper.selectById(id);
    }

    @Override
    public PageDataVO<TeamVO> queryCurrentTeams(PageParam pageParam) {
        List<TeamMember> teamMembers = teamMemberMapper
                .selectList(new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getUserId, UserContext.get().getId()));
        List<Integer> teamIds = teamMembers.stream().map(TeamMember::getTeamId).collect(Collectors.toList());
        Page<TeamVO> infoVOPage = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        Page<TeamVO> teamVOPage = teamVOMapper.selectPage(
                infoVOPage,
                new LambdaQueryWrapper<TeamVO>()
                        .in(TeamVO::getId, teamIds)
                        .orderByDesc(TeamVO::getId));
        return PageUtil.convert(teamVOPage);
    }

    @Override
    public List<TeamVO> queryMyLeadingTeam() {
        LambdaQueryWrapper<TeamVO> queryWrapper = new LambdaQueryWrapper<TeamVO>()
                .eq(TeamVO::getCreatorId, UserContext.get().getId());
        return teamVOMapper.selectList(queryWrapper);
    }
}
